#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
# @FileName  :session.py
# @Time      :2022/4/20 4:49 PM
# @Author    :Toliong toliong@foxmail.com
# @packages  :pip install slqalchemy psycopg2
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from core.config import settings
from typing import Generator
# import sys


SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL
engine = create_engine(SQLALCHEMY_DATABASE_URL)

# SQLite config
# SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# engine = create_engine(
#     SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
# )


SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


def get_db() -> Generator:
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()


if __name__ == "__main__":
    pass
    # SessionLoal.object_session()

